<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//请求参数对象
				queryObj:{
					query:'',
					cid:'',
					pagenum:1,
					pagesize:10
				},
				goodsList:[],
				total:0,
				isLoading:false
			};
		},
		onLoad(option) {
			
			this.queryObj.query=option.query || ''
			this.queryObj.cid=option.cid || ''
			
			this.getGoodsList()
		},
		onReachBottom() {
			if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total)return uni.showToast({title:'数据加载完毕'})
			if(this.isLoading)return
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		onPullDownRefresh() {
			//重置数据
			this.queryObj.pagenum=1
			this.total=0
			this.isLoading=false
			this.goodsList=[]
			this.getGoodsList(()=>{uni.stopPullDownRefresh()})
		},
		methods:{
			gotoDetail(goods){
	
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id='+goods.goods_id
				});
			},
			getGoodsList(cb){
				this.isLoading=true
				var that = this
				uni.request({
					url:'https://www.uinav.com/api/public/v1/goods/search',
					method:'GET',
					data:this.queryObj,
					success({data:res}) {
						uni.showToast({
							title:'请求成功',
							icon:'none'
						})	
						that.goodsList=[...that.goodsList,...res.message.goods]
						that.total=res.message.total
						console.log(that.goodsList);
						that.isLoading=false
						cb&&cb()
					},
					fail() {
						uni.showToast({
							title:'请求失败',
							icon:'none'
						})
					}
					
				})
				
			}
		}
	}
</script>

<style lang="scss">
	
</style>
